package platformer;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.ArrayList;
import java.util.Scanner;
import javax.imageio.ImageIO;

public class Manager {

	public boolean[] keysDown = new boolean[256];
	public chars.Player player;
	public ArrayList<chars.platform> platforms;
	public int level;
	Panel panel;
	int lastY;
	int firstY;
	boolean starting;
	private Image bg;
	private Image topBar;
	public ArrayList<String> levels;
	public ArrayList<chars.hittable> hittables;
	public ArrayList<chars.bg> bgItems;
	private static ArrayList<chars.hittable> hittablesSave;
	private static ArrayList<chars.bg> bgItemsSave;
	private static ArrayList<chars.platform> platformsSave;
	private static chars.Player playerSave;
	private int[] playerLocSave;
	public boolean win;
	Scanner getText;

	public Manager(Panel panel) {
		playerLocSave = new int[2];
		level = 5000;
		playerSave = new chars.Player();
		hittablesSave = null;
		bgItemsSave = null;
		platformsSave = null;
		levels = new ArrayList<String>();
		this.panel = panel;
		for (boolean i : keysDown) {
			i = false;
		}
		player = new chars.Player();
		starting = true;
		try {
			topBar = ImageIO.read(new File("Images/graphics/topbar.png"));
		} catch (Exception e) {
		}
		firstY = 0;
	}

	public void move(int x, int y) {
		player.x += x;
		player.y += y;
		for (int i = 0; i < bgItems.size(); i++) {
			bgItems.get(i).x += x;
			bgItems.get(i).y += y;
		}
		for (int i = 0; i < platforms.size(); i++) {
			platforms.get(i).x += x;
			platforms.get(i).y += y;
		}
		lastY += y;
		firstY += y;
		for (int i = 0; i < hittables.size(); i++) {
			hittables.get(i).x += x;
			hittables.get(i).y += y;
		}
	}

	public void move() {
		int change = 0;
		if (player.x + player.appearance.getWidth(null) > 450) {
			change = -(int) Math.pow(1.01, player.x
					+ player.appearance.getWidth(null) - 450);
		} else {
			if (player.x < 150) {
				change = (int) Math.pow(1.01, 150 - player.x);
			}
		}
		for (int i = 0; i < bgItems.size(); i++) {
			bgItems.get(i).x += change;
		}
		for (int i = 0; i < hittables.size(); i++) {
			hittables.get(i).x += change;
		}
		player.x += change;
		for (int i = 0; i < platforms.size(); i++) {
			platforms.get(i).x += change;
		}
		change = 0;
		if (player.y < 125) {
			change = (int) Math.pow(1.01, 125 - player.y);
		} else {
			if (player.y + player.appearance.getHeight(null) > 175) {
				change = -(int) Math.pow(1.01, player.y
						+ player.appearance.getHeight(null) - 175);
			}
		}
		for (int i = 0; i < bgItems.size(); i++) {
			bgItems.get(i).y += change;
		}
		for (int i = 0; i < hittables.size(); i++) {
			hittables.get(i).y += change;
		}
		player.y += change;
		for (int i = 0; i < platforms.size(); i++) {
			platforms.get(i).y += change;
		}
		lastY += change;
		firstY += change;
	}

	public void draw(Graphics g, Panel p) {
		move();
		player.move(keysDown);
		firstY = 0;
		int x = player.x;
		int y = player.y;
		g.drawImage(bg, 0, 0, null);
		if (player.y > lastY || keysDown[82] || player.y < firstY - 50) {
			try {
				setLevel(level);
			} catch (java.io.IOException e) {
				System.err.println(e.getMessage());
			}
		}
		for (int i = 0; i < platforms.size(); i++) {
			if (platforms.get(i).draw(g, p)) {
				platforms.get(i).checkCollision(player);
			}
		}
		for (int i = 0; i < hittables.size(); i++) {
			if (hittables.get(i).draw(g)) {
				if (hittables.get(i).checkCollision(player)) {
					hittables.get(i).act(this);
				}
			}
		}
		for (int i = 0; i < bgItems.size(); i++) {
			bgItems.get(i).draw(g);
		}
		if (player.dead) {
			player.x = x;
			player.y = y;
		}
		g.drawImage(topBar, 0, 0, null);
		g.drawString("(" + player.x + "," + player.y + ")", 15, 20);
		player.draw((Graphics2D) g, p, keysDown, platforms);
		if (win) {
			win = false;
			try {
				if (levels.size() > level) {
					setLevel(level + 1);
				} else {
					win = true;
				}
			} catch (Exception e) {
			}
		}
	}

	public void setScanner(Scanner levelpack) {
		while (levelpack.hasNextLine()) {
			levels.add(levelpack.nextLine());
		}
	}

	public void setLevel(int level) throws java.io.IOException {
		win = false;
		player.dead = false;
		int playerX;
		int playerY;
//		if (this.level != level) {
			boolean upPlat = false;
			boolean downPlat = false;
			boolean rightPlat = false;
			boolean leftPlat = false;
			this.level = level;
			bg = ImageIO.read(new File("Images/bgs/default.png"));
			if (starting) {
				starting = false;
			}
			Graphics g = panel.getGraphics();
			g.drawImage(ImageIO.read(new File("Images/graphics/loading.png")),
					200, 110, null);
			g.drawRect(199, 201, 202, 3);
			g.setColor(Color.orange);
			g.drawString("Loading " + levels.get(level), 250, 230);
			player.xSpeed = 0;
			player.ySpeed = 0;
			platforms = new ArrayList<chars.platform>();
			hittables = new ArrayList<chars.hittable>();
			bgItems = new ArrayList<chars.bg>();
			File file = new File("Images/levels/" + levels.get(0) + ".png");
			BufferedImage levelImg = ImageIO.read(file);
			try {
				file = new File("Images/levels/" + levels.get(level) + ".png");
				levelImg = ImageIO.read(file);
			} catch (Exception e) {
				System.out.println("Next Level Not Found!!!");
				file = new File("Images/levels/lvl1.png");
				levelImg = ImageIO.read(file);
			}
			try {
				getText = new Scanner(new File("Text/" + levels.get(level)
						+ ".txt"));
			} catch (Exception e) {
			}
			int width = levelImg.getWidth(null);
			int heigth = levelImg.getHeight(null);
			int rgb;
			int red;
			int blue;
			int green;
			boolean made;
			for (int i = 0; i < width; i++) {
				g.setColor(Color.red);
				g.drawLine(200, 202, 200 + 200 * i / width, 202);
				g.drawLine(200, 203, 200 + 200 * i / width, 203);
				for (int q = 0; q < heigth; q++) {
					made = false;
					rgb = levelImg.getRGB(i, q);
					red = (rgb & 0x00ff0000) >> 16;
					green = (rgb & 0x0000ff00) >> 8;
					blue = rgb & 0x000000ff;
					if (red == 255 && green == 0 && blue == 0) {
						player.x = i * 10;
						playerLocSave[0] = player.x;
						player.y = q * 10;
						playerLocSave[1] = player.y;
					}
					upPlat = false;
					downPlat = false;
					rightPlat = false;
					leftPlat = false;
					if (q < heigth - 1) {
						rgb = levelImg.getRGB(i, q + 1);
						red = (rgb & 0x00ff0000) >> 16;
						green = (rgb & 0x0000ff00) >> 8;
						blue = rgb & 0x000000ff;
						if (green == 255 && blue == 0 && red == 0) {
							downPlat = true;
						}
					}
					if (q > 0) {
						rgb = levelImg.getRGB(i, q - 1);
						red = (rgb & 0x00ff0000) >> 16;
						green = (rgb & 0x0000ff00) >> 8;
						blue = rgb & 0x000000ff;
						if (green == 255 && blue == 0 && red == 0) {
							upPlat = true;
						}
					}
					if (i < width - 1) {
						rgb = levelImg.getRGB(i + 1, q);
						red = (rgb & 0x00ff0000) >> 16;
						green = (rgb & 0x0000ff00) >> 8;
						blue = rgb & 0x000000ff;
						if (green == 255 && blue == 0 && red == 0) {
							rightPlat = true;
						}
					}
					if (i > 0) {
						rgb = levelImg.getRGB(i - 1, q);
						red = (rgb & 0x00ff0000) >> 16;
						green = (rgb & 0x0000ff00) >> 8;
						blue = rgb & 0x000000ff;
						if (green == 255 && blue == 0 && red == 0) {
							leftPlat = true;
						}
					}
					rgb = levelImg.getRGB(i, q);
					red = (rgb & 0x00ff0000) >> 16;
					green = (rgb & 0x0000ff00) >> 8;
					blue = rgb & 0x000000ff;
					if (green == 255 && red == 0 && blue == 0) {
						if (downPlat) {
							if (upPlat) {
								if (rightPlat) {
									if (leftPlat) {
										platforms.add(new chars.platform(
												i * 10, q * 10, 4));
									} else {
										platforms.add(new chars.platform(
												i * 10, q * 10, 8));
									}
								} else {
									if (leftPlat) {
										platforms.add(new chars.platform(
												i * 10, q * 10, 9));
									} else {
										platforms.add(new chars.platform(
												i * 10, q * 10, 10));
									}
								}
							} else {
								if (rightPlat) {
									if (leftPlat) {
										platforms.add(new chars.platform(
												i * 10, q * 10, 11));
									} else {
										platforms.add(new chars.platform(
												i * 10, q * 10, 12));
									}
								} else {
									if (leftPlat) {
										platforms.add(new chars.platform(
												i * 10, q * 10, 13));
									} else {
										platforms.add(new chars.platform(
												i * 10, q * 10, 3));
									}
								}
							}
						} else {
							if (upPlat) {
								if (rightPlat) {
									if (leftPlat) {
										platforms.add(new chars.platform(
												i * 10, q * 10, 14));
									} else {
										platforms.add(new chars.platform(
												i * 10, q * 10, 15));
									}
								} else {
									if (leftPlat) {
										platforms.add(new chars.platform(
												i * 10, q * 10, 16));
									} else {
										platforms.add(new chars.platform(
												i * 10, q * 10, 2));
									}
								}
							} else {
								if (rightPlat) {
									if (leftPlat) {
										platforms.add(new chars.platform(
												i * 10, q * 10, 5));
									} else {
										platforms.add(new chars.platform(
												i * 10, q * 10, 7));
									}
								} else {
									if (leftPlat) {
										platforms.add(new chars.platform(
												i * 10, q * 10, 6));
									} else {
										platforms.add(new chars.platform(
												i * 10, q * 10, 1));
									}
								}
							}
						}
					}
					rgb = levelImg.getRGB(i, q);
					red = (rgb & 0x00ff0000) >> 16;
					green = (rgb & 0x0000ff00) >> 8;
					blue = rgb & 0x000000ff;
					if (green == 255 && red == 255 && blue == 0) {
						hittables.add(new chars.exitLevel(i * 10, q * 10));
					}
					if (green == 0 && red == 0 && blue == 255) {
						hittables.add(new chars.mine(i * 10, q * 10));
					}
					if (green == 0 && red == 0 && blue == 0) {
						try {
							if (getText.hasNextLine()) {
								bgItems.add(new chars.message(getText
										.nextLine(), i * 10, q * 10));
							}
						} catch (Exception e) {
						}
					}
				}
			}
			lastY = levelImg.getHeight(null) * 10;
			playerSave = player;
			platformsSave = platforms;
			hittablesSave = hittables;
			bgItemsSave = bgItems;
//		} else {
//			player = playerSave;
//			player.x = playerLocSave[0];
//			player.y = playerLocSave[1];
//			platforms = platformsSave;
//			hittables = hittablesSave;
//			bgItems = bgItemsSave;
//
//		}
		boolean discovered = true;
		while (discovered) {
			discovered = false;
			if (player.x > 450) {
				move(-5, 0);
				discovered = true;
			}
			if (player.y > 250) {
				move(0, -3);
				discovered = true;
			}
		}
		keysDown[82] = false;

	}
}